package com.ruoyi.system.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 原料仓库信息Controller
 * 
 * @author ruoyi
 * @date 2023-12-31
 */
@Controller
@RequestMapping("/system/warehouse")
public class WarehouseController extends BaseController
{
    private String prefix = "system/warehouse";
    @Autowired
    private ITeaShopService teaShopService;

    @Autowired
    private IWarehouseService warehouseService;

    @Autowired
    private IWarehouseRawMaterialService warehouseRawMaterialService;
    @Autowired
    private IExpendService expendService;
    @Autowired
    private IWarehouseInventoryService warehouseInventoryService;

    @RequiresPermissions("system:warehouse:view")
    @GetMapping()
    public String warehouse()
    {
        return prefix + "/warehouse";
    }

    /**
     * 查询原料仓库信息列表
     */
    @RequiresPermissions("system:warehouse:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Warehouse warehouse)
    {
        startPage();
        List<Warehouse> list = warehouseService.selectWarehouseList(warehouse);
        return getDataTable(list);
    }

    /**
     * 导出原料仓库信息列表
     */
    @RequiresPermissions("system:warehouse:export")
    @Log(title = "原料仓库信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Warehouse warehouse)
    {
        List<Warehouse> list = warehouseService.selectWarehouseList(warehouse);
        ExcelUtil<Warehouse> util = new ExcelUtil<Warehouse>(Warehouse.class);
        return util.exportExcel(list, "原料仓库信息数据");
    }

    /**
     * 新增原料仓库信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存原料仓库信息
     */
    @RequiresPermissions("system:warehouse:add")
    @Log(title = "原料仓库信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Warehouse warehouse)
    {
        return toAjax(warehouseService.insertWarehouse(warehouse));
    }

    /**
     * 修改原料仓库信息
     */
    @RequiresPermissions("system:warehouse:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Warehouse warehouse = warehouseService.selectWarehouseById(id);
        mmap.put("warehouse", warehouse);
        return prefix + "/edit";
    }

    /**
     * 修改保存原料仓库信息
     */
    @RequiresPermissions("system:warehouse:edit")
    @Log(title = "原料仓库信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Warehouse warehouse)
    {
        return toAjax(warehouseService.updateWarehouse(warehouse));
    }

    /**
     * 删除原料仓库信息
     */
    @RequiresPermissions("system:warehouse:remove")
    @Log(title = "原料仓库信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(warehouseService.deleteWarehouseByIds(ids));
    }

    @GetMapping("/warehouseInventory/{warehouseId}/{teaShopId}")
    public String warehouseInventory(@PathVariable("warehouseId") Long warehouseId, @PathVariable("teaShopId") Long teaShopId, ModelMap mmap)
    {
        mmap.put("warehouse", warehouseService.selectWarehouseById(warehouseId));
        TeaShop teaShop = teaShopService.selectTeaShopById(teaShopId);
        if (teaShop == null) {
            teaShop = new TeaShop();
            teaShop.setAddress("未选择地址");
        }
        mmap.put("teaShop", teaShop);
        return prefix + "/warehouseInventory";
    }
    /**
     * 查询仓库库存列表
     */
    @PostMapping("/warehouseInventory/warehouseInventoryList")
    @ResponseBody
    public TableDataInfo warehouseInventoryList(WarehouseInventory warehouseInventory)
    {
        startPage();
        List<WarehouseInventory> list = warehouseService.selectInventoryList(warehouseInventory);
        return getDataTable(list);
    }

    @GetMapping("/warehouseInventory/chooseTeaShop/{warehouseId}")
    public String chooseTeaShop(@PathVariable("warehouseId") Long warehouseId, ModelMap mmap)
    {
        mmap.put("warehouse", warehouseService.selectWarehouseById(warehouseId));
        return prefix + "/chooseTeaShop";
    }

    @PostMapping("/warehouseInventory/chooseTeaShop/chooseTeaShopList")
    @ResponseBody
    public TableDataInfo chooseTeaShopList(TeaShop teaShop)
    {
        startPage();
        List<TeaShop> list = teaShopService.selectTeaShopList(teaShop);
        return getDataTable(list);
    }


    @GetMapping("/warehouseInventory/deliverMaterial/{warehouseId}/{teaShopId}")
    public String deliverMaterial(@PathVariable("warehouseId") Long warehouseId, @PathVariable("teaShopId") Long teaShopId, ModelMap mmap)
    {
        mmap.put("warehouse", warehouseService.selectWarehouseById(warehouseId));
        mmap.put("teaShop", teaShopService.selectTeaShopById(teaShopId));
        List<WarehouseRawMaterial> list = warehouseRawMaterialService.selectWarehouseRawMaterialList(new WarehouseRawMaterial());
        mmap.put("materialAmounts", list.size());
        return prefix + "/deliver";
    }

    @PostMapping("/warehouseInventory/deliverMaterial/selectDeliverMaterial")
    @ResponseBody
    public AjaxResult selectDeliverMaterial(Long warehouseId, Long teaShopId, String materialIds, String data)
    {
        String[] strArray = data.split(",");
        String deliverAmounts = "";
        for (int i = 0; i < strArray.length; i++) {
            if (!strArray[i].equals("")) {
                deliverAmounts = deliverAmounts + strArray[i];
                if (i < strArray.length - 1)
                    deliverAmounts = deliverAmounts + ",";
            }
        }
        return toAjax(warehouseService.insertDeliverMaterial(warehouseId, teaShopId, materialIds, deliverAmounts));
    }

    @GetMapping("/warehouseInventory/importMaterial/{warehouseId}")
    public String importMaterial(@PathVariable("warehouseId") Long warehouseId, ModelMap mmap)
    {
        System.out.println(warehouseId);
        mmap.put("warehouse", warehouseService.selectWarehouseById(warehouseId));
        long materialAmounts = warehouseRawMaterialService.selectWarehouseRawMaterialList(new WarehouseRawMaterial()).size();
        mmap.put("materialAmounts", materialAmounts);
        return prefix + "/importMaterial";
    }

    @PostMapping("/warehouseInventory/importMaterial/importMaterialSubmit")
    @ResponseBody
    public AjaxResult importMaterialSubmit(Long warehouseId, String materialIds, String importAmounts)
    {
        Date currentTime = new Date();
        String[] materialIdArray = materialIds.split(",");
        String[] importAmountArray = importAmounts.split(",");
        List<WarehouseInventory> warehouseInventories = warehouseInventoryService.selectWarehouseInventoryList(new WarehouseInventory());
        for (int i = 0; i < materialIdArray.length; i++) {
            int index = Integer.parseInt(materialIdArray[i]);
            WarehouseRawMaterial material = warehouseRawMaterialService.selectWarehouseRawMaterialById((long) Integer.parseInt(materialIdArray[i]));
            int warehouseInventoryId = -1;
            for (int j = 0; j < warehouseInventories.size(); j++) {
                if (warehouseInventories.get(j).getWarehouseId() == warehouseId) {
                    if (warehouseInventories.get(j).getRawMaterialId() == (long) Integer.parseInt(materialIdArray[i])) {
                        warehouseInventoryId = j;
                        break;
                    }
                }
            }

            if (warehouseInventoryId == -1) {
                WarehouseInventory warehouseInventory = new WarehouseInventory();
                warehouseInventory.setWarehouseId(warehouseId);
                warehouseInventory.setRawMaterialId((long) Integer.parseInt(materialIdArray[i]));
                warehouseInventory.setAmount(Long.valueOf(importAmountArray[index]));
                warehouseInventoryService.insertWarehouseInventory(warehouseInventory);
            } else {
                WarehouseInventory temp = warehouseInventoryService.selectWarehouseInventoryById(warehouseInventories.get(warehouseInventoryId).getId());
                temp.setAmount(temp.getAmount() + Long.parseLong(importAmountArray[index]));
                warehouseInventoryService.deleteWarehouseInventoryById(temp.getId());
                warehouseInventoryService.insertWarehouseInventory(temp);
            }
            Expend expend = new Expend();
            expend.setType("2");
            expend.setCost(material.getCost() * Integer.parseInt(importAmountArray[index]));
            expend.setDescription(material.getSupplier() + "的" + material.getName() + "进货数量:" + importAmountArray[index]);
            expend.setDate(currentTime);
            expendService.insertExpend(expend);
        }
        return toAjax(1);
    }
}
